package butines.core.game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;

import butines.core.math2d.Vector2D;

public class Drawing {
	
	public static int LEFT   = -1;
	public static int CENTER = 0;
	public static int RIGHT  = 1;
	
	public static int TOP    = 1;
	public static int MIDDLE = 0;
	public static int BOTTOM = -1;
	
	private static Display display;
	
	public static void setDisplay(Display display) {
		Drawing.display = display;
	}
	
	public static Graphics2D getGraphics() {
		return display.getGraphics();
	}
	
	public static void circle(Vector2D s, float r) {
		circle(s.x, s.y, r);
	}
	
	public static void circle(float sx, float sy, float r) {
		circle(sx, sy, r, false);
	}
	
	public static void circle(float sx, float sy, float r, boolean fill) {
		int x = Math.round(sx - r);
		int y = Math.round(sy - r);
		int wh = Math.round(r * 2);
		if (fill) {
			getGraphics().fillOval(x, y, wh, wh);
		} else {
			getGraphics().drawOval(x, y, wh, wh);
		}
	}
		
	public static void rect(float sx, float sy, float w, float h) {
		
	}
	
	public static void rect(float sx, float sy, float w, float h, boolean fill) {
		int x = Math.round(sx);
		int y = Math.round(sy);
		int width = Math.round(w);
		int height = Math.round(h);
		if (fill) {
			getGraphics().fillRect(x, y, width, height);
		} else {
			getGraphics().drawRect(x, y, width, height);
		}
	}
	
	public static void line(float sx, float sy, float dx, float dy) {
		int x1 = Math.round(sx);
		int y1 = Math.round(sy);
		int x2 = Math.round(dx);
		int y2 = Math.round(dy);
		getGraphics().drawLine(x1, y1, x2, y2);
	}
	
	public static void line(Vector2D s, Vector2D d) {
		line(s.x,  s.y, d.x, d.y);
	}

	public static void setColor(Color c) {
		getGraphics().setColor(c);
	}

	public static void setFont(Font f) {
		getGraphics().setFont(f);
	}

	public static int getFontWidth(String str) {
		return getGraphics().getFontMetrics().stringWidth(str);
	}
	
	public static int getFontHeight() {
		return getGraphics().getFontMetrics().getHeight();
	}
	
	public static void clear(Color color) {
		getGraphics().clearRect(0, 0, display.getWidth(), display.getHeight());
	}
	
	private static float halign(float x, float w, int halign) {
		
		if (halign > 0) {
			return x - w;
		}
		if (halign == 0) {
			return x - w / 2;
		}
		return x;
	}

	private static float valign(float y, float h, int valign) {
		if (valign > 0) {
			return y + h;
		}
		if (valign == 0) {
			return y + h / 2;
		}
		return y;
	}
	
	public static void print(float sx, float sy, int halign, int valign, String str) {
		int w = getFontWidth(str);
		int h = getFontHeight();
		getGraphics().drawString(str, halign(sx, w, halign), valign(sy, h, valign));
	}
	
	public static void draw(Image image, float sx, float sy) {
		image.draw(getGraphics(), sx, sy);
	}
	
	public static void draw(Image image, float sx, float sy, float angle, float scale) {
		image.draw(getGraphics(), sx, sy, angle, scale);
	}
	
}
